﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using AppCommon;
using Controller;
using Model;

public partial class ACPanel_NodeTour_QLNgayKhoiHanh : System.Web.UI.Page
{
    public static SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["defaultConnection"].ConnectionString);
    private static string _id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var idTour = Request.QueryString["Id"];
            if (idTour != null)
            {
                _id = idTour;
                FillGrid("NgayCapNhat", false, idTour);    
            }            
        }
    }

    /// <summary>
    /// Method : FillGrid 
    /// Use : Bind data to gridview
    /// </summary>
    /// <param name="SortExpression"></param>
    /// <param name="IsSorting"></param>
    public void FillGrid(string SortExpression, bool IsSorting, string idTour)
    {
        var dt = new DataTable();        
        // Lay chuoi ket noi CSDL
        Connection.Open();
        var query = "SELECT * FROM [VYC-01-01] WHERE IdTour like '"+ idTour +"' ORDER BY NgayCapNhat DESC";
        var dataAdapter = new SqlDataAdapter(query, Connection);
        dataAdapter.Fill(dt);
        Connection.Close(); // dong ket noi       

        var dv = dt.DefaultView;
        if (ViewState["SortExpression"] != null && ViewState["SortDirection"] != null)
        {
            if (IsSorting)
            {
                if (ViewState["SortExpression"].ToString() == SortExpression)
                {
                    if (ViewState["SortDirection"].ToString() == "DESC")
                        ViewState["SortDirection"] = "ASC";
                    else
                        ViewState["SortDirection"] = "DESC";
                }
                else
                {
                    ViewState["SortExpression"] = SortExpression;
                    ViewState["SortDirection"] = "DESC";
                }
            }
        }
        else
        {
            ViewState["SortExpression"] = SortExpression;
            ViewState["SortDirection"] = "DESC";
        }

        dv.Sort = ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        gv.DataSource = dv.ToTable();
        gv.DataBind();
    }

    /// <summary>
    /// Event OnPageIndexChanging="gv_PageIndexChanging" on GridView
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv.PageIndex = e.NewPageIndex;
        FillGrid(ViewState["SortExpression"].ToString(), false, _id);
    }

    /// <summary>
    /// Event OnSorting="gv_Sorting" on GridView
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_Sorting(object sender, GridViewSortEventArgs e)
    {
        FillGrid(e.SortExpression, true, _id);
    }

    protected void btnThemvao_OnClick(object sender, EventArgs e)
    {
        try
        {
            var controller = new VYC0101Controller();
            var model = new ModelVYC0101();

            if (_id != null && _id != "")
            {
                model.IdTour = _id;
                model.NgayGioDi = DateTime.Parse(tbNgaydi.Text);
                model.NgayGioDen = DateTime.Parse(tbNgayden.Text);
                model.GiaTour = Convert.ToInt32(tbGiatour.Text);
                var info = (ModelVYC17)Session[Constants.SESSION_ADMIN];
                model.IdUser = info.UserName; // Get in session
                model.TrangThai = Convert.ToInt32(cbTrangThai.Checked);

                var isSuccess = 0;
                isSuccess = controller.AddData(model);                

                if (isSuccess == 0)
                {
                    // Chua thanh cong                    
                    litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Thêm dữ liệu thất bại.</p>";
                }
                else
                {                    
                    litResult.Text = "<p class=\"alert alert-success\">Thêm dữ liệu thành công.</p>";
                    FillGrid(ViewState["SortExpression"].ToString(), false, _id);
                    ResetInput();
                }
            }
            else
            {
                Response.Redirect("~/ACPanel/Error.aspx");
            }
        }
        catch (Exception ex)
        {
            litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Thêm dữ liệu thất bại. Message : " + ex.Message + "</p>";
            throw;
        }
    }

    public void ResetInput()
    {        
        tbNgaydi.Text = "";
        tbNgayden.Text = "";        
        tbGiatour.Text = "";
        cbTrangThai.Checked = false;
    }
}